package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TemplatesService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("templates")
public class TemplagesController {
    private Logger logger = LoggerFactory.getLogger(BrandController.class);
    @Reference
    private TemplatesService templatesService;
    @Reference
    private SpecificationService specificationService;
    @Reference
    private BrandService brandService;

    @RequestMapping("toTypeTemplates")
    public String toTypeTemplates(){
        return "admin/type_template";
    }

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(String sname,@RequestParam(defaultValue = "1")Integer number,@RequestParam(defaultValue = "3")Integer pageSize){
        try {
            PageBean<TbTypeTemplate> b = templatesService.getinfo(sname,number,pageSize);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),b);
        }catch (Exception e){
            logger.error("增加失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toAdd")
    @ResponseBody
    public Map<String,Object> toAdd(){
        List<TbBrand> blist = brandService.getAll();
        List<TbSpecification> slist = specificationService.getAll();
        Map<String,Object> map = new HashMap<>();
        map.put("blist",blist);
        map.put("slist",slist);
        return map;
    }

    @RequestMapping("addinfo")
    @ResponseBody
    public dataResult addinfo(TbTypeTemplate t,String brands,String specs,String items){
        try {
            t.setBrandIds(brands);
            t.setSpecIds(specs);
            t.setCustomAttributeItems(items);
            Integer i = templatesService.addinfo(t);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("增加失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public dataResult deleteAll(@RequestParam("ids[]") Long[] ids){
        try {
            Integer i = templatesService.deleteAll(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("增加失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public dataResult toUpdate(Long id){
        try {
            TbTypeTemplate template = templatesService.selectById(id);
            List<TbBrand> blist = brandService.getAll();
            List<TbSpecification> slist = specificationService.getAll();
            Map<String,Object> map = new HashMap<>();
            map.put("blist",blist);
            map.put("slist",slist);
            map.put("tem",template);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),map);
        }catch (Exception e){
            logger.error("回显失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("updateinfo")
    @ResponseBody
    public dataResult updateinfo(TbTypeTemplate t){
        try {
            Integer i = templatesService.updateinfo(t);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("增加失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }
}
